//
//  CustomBrandTableViewCell.m
//  ChunLaiZhuangClient
//
//  Created by zzs on 16/3/2.
//  Copyright © 2016年 CLZ. All rights reserved.
//

#import "CustomBrandTableViewCell.h"
#import "BrandCategoryViewController.h"
@implementation CustomBrandTableViewCell

+ (instancetype)cellWithTableView:(UITableView *)tableView{
    static NSString *identifier = @"status";
    // 1.缓存中取
    CustomBrandTableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    // 2.创建
    if (cell == nil) {
        cell = [[CustomBrandTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    return cell;
}
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.contentView.backgroundColor=[UIColor colorWithHexString:@"efeff4"];
        
                
    }
    return self;
}
-(void)setFrameModel:(BrandFrameModel *)frameModel{
    _frameModel=frameModel;
    if (!self.buttonsArray) {
        self.buttonsArray=[[NSMutableArray alloc] init];
    }else{
        if (self.buttonsArray.count>0) {
            for(UIButton *bt in self.buttonsArray){
                [bt removeFromSuperview];
            }
        }
    }
    
    float edge=9.0;
    float widh=(SCREEN_WIDTH-18-2*3)/4;
    float distance=2;
    for (int i=0; i<frameModel.model.dataArray.count; i++) {
        BrandGroupDetailSubModel *model=frameModel.model.dataArray[i];
        UIButton *tagBt=[UIButton buttonWithType:UIButtonTypeCustom];
        tagBt.frame=CGRectMake(0, 0, widh, widh);
        tagBt.left=edge+(widh+distance)*(i%4);
        tagBt.top=(tagBt.height+2)*(i/4);
        tagBt.tag=i;
        [tagBt sd_setImageWithURL:[NSURL URLWithString:model.image] forState:UIControlStateNormal placeholderImage:nil];
        [tagBt addTarget:self action:@selector(tagAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:tagBt];
        [self.buttonsArray addObject:tagBt];

    }

}
-(void)tagAction:(UIButton*)bt{
    UIViewController *currentVC=[CustomBrandTableViewCell viewController:self];
    BrandGroupDetailSubModel *model=_frameModel.model.dataArray[bt.tag];
    BrandCategoryViewController *brandVC=[[BrandCategoryViewController alloc] init];
    [brandVC setBrandDetailModel:model];
    [currentVC.navigationController pushViewController:brandVC animated:YES];
}

@end